var search=location.search
var arr=/id=(\d+)/.exec(search)
if(!arr){
    layer.msg('非法请求',{
        icon:2,
        time:1500
    },function(){
        location.href='./list.html'
    })
}
var id=arr[1]
// console.log(id)
var loadindex=layer.load(1,{
    shade:[0.9,'#000']
})
$.get('./php/detail.php',{id},res=>{
    console.log(res)
    layer.close(loadindex)
    var {meta:{status,msg},data}=res;
    if(status===1){
        $('.goodsname span').text(data.name)
        $('.goodssprice .price1').text(data.price)
        $('.goodssprice .price2').text(data.introduce)
        var smallimagarr=data.imgpath.split('=====')
        var img='';
        smallimagarr.forEach((item,index)=>{
            if(index===0){
                img+=`
                <a href="javascript:;"><img class="active" src="${item}"></a>
            `
            $('.box>img').attr('src',item)
            $('.big>img').attr('src',item)
            }else{
                img+=`
                <a href="javascript:;"><img src="${item}"></a>
            `
            }
            
        })
        $('.small').html(img)
        
        enlarge()
        addcart()
    }
},'json')


// 加入购物车
function addcart(){
    $('.shoping').click(function(){
        var username=tool.getCookie('username')
        console.log(username)
        if(!username){
            layer.msg("请登录",{
                icon:2,
                item:1500
            },function(){
                localStorage.setItem('url',location.href)
                location.href="./登录.html"
            })
            return false
        }
        var data=localStorage.getItem('data')
        if(data){
            var arr=JSON.parse(data)
            var obj=arr.find(item=>item.username === username && item.goodsid===id)
            if(obj){
                obj.number+=1
            }else{
                var obj={
                goodsid:id,
                username,
                number:1,
                }
                arr.push(obj)
            }
        }else{
            var arr=[];
            var obj={
                goodsid:id,
                username,
                number:1,
                }
            arr.push(obj)
        }
        
        // 存储arr
        localStorage.setItem('data',JSON.stringify(arr))
    })
}










